#include "colormanager.h"
#include <cstdlib>

/*
  Returns a ramdom color from the line color sets:
  0. White          (255, 255, 255)
  1. Blue           (  0,   0, 255)
  2. Purple         (255,   0, 255)
  3. Cyan           (  0, 255, 255)
  4. Pink           (255, 192, 203)
  5. Brown          (165,  42,  42)
  6. Gray           (190, 190, 190)
  7. Gold           (255, 215,   0)
  8. Dark Gray      ( 49,  79,  79)
  9. Light Gray     (211, 211, 211)
  10.Navy           (  0,   0, 128)
  11.Sky            (135, 206, 250)
  12.Steel          ( 70, 130, 180)
  13.Dark Green     (  0, 100,   0)
  14.Olive          ( 85, 107,  47)
  15.Indian Red     (205,  92,  92)
*/
const QColor ColorManager::definedLineColor()
{
    QTime midnight(0, 0, 0);
    qsrand(midnight.secsTo(QTime::currentTime()));
    switch (qrand()%16)
    {
    case 0: return QColor(  0,   0,   0); break;
    case 1: return QColor(  0,   0, 255); break;
    case 2: return QColor(255,   0, 255); break;
    case 3: return QColor(  0, 255, 255); break;
    case 4: return QColor(255, 192, 203); break;
    case 5: return QColor(165,  42,  42); break;
    case 6: return QColor(190, 190, 190); break;
    case 7: return QColor(255, 215,   0); break;
    case 8: return QColor( 49,  79,  79); break;
    case 9: return QColor(211, 211, 211); break;
    case 10: return QColor(  0,   0, 128); break;
    case 11: return QColor(135, 206, 250); break;
    case 12: return QColor( 70, 130, 180); break;
    case 13: return QColor(  0, 100,   0); break;
    case 14: return QColor( 85, 107,  47); break;
    case 15: return QColor(205,  92,  92); break;
    }
    return QColor();
}

const QColor ColorManager::randomLineColor()
{
    QColor c;
    do {
        c = randomColor();
    } while (near(c, Qt::yellow) || near(c, Qt::black) || near(c, Qt::green) || near(c, Qt::red));
    return c;
}

const QColor ColorManager::randomColor()
{
    QTime midnight(0, 0, 0);
    qsrand(midnight.secsTo(QTime::currentTime()));
    return QColor(qrand()%256, qrand()%256, qrand()%256);
}

bool ColorManager::near(const QColor &color1, const QColor &color2)
{
    int dr = color1.red()>color2.red()? color1.red()-color2.red() : color2.red()-color1.red();
    int dg = color1.green()>color2.green()? color1.green()-color2.green() : color2.green()-color1.green();
    int db = color1.blue()>color2.blue()? color1.blue()-color2.blue() : color2.blue()-color1.blue();

    if (dr < 45 && dg < 45 && db < 45)
        return true;
    else return false;
}

